<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        ul {
            list-style: none;
            display: flex;
            line-height: 35px;
        }

        ul li {
            padding: 0 10px;
            border: 1px solid black;
            margin-right: 20px;
        }

        .box {
            position: relative;
            width: 500px;
            margin: 20px auto;
        }

        .box>div {
            position: absolute;
            top: 36px;
            left: 0;
            width: 500px;
            height: 150px;
            display: none;
            z-index: -1;
            border: 1px solid red;
            padding: 10px;

        }

        .box .active {
            display: block;

        }

        .box ul .active {
            border-bottom: 1px solid #fff;
        }
    </style>
</head>

<body>
    <div class="box">
        <ul>
            <li class="active">编程</li>
            <li>读书</li>
            <li>运动</li>
        </ul>
        <div class="active">编程使我快乐</div>
        <div>读书使我幸福</div>
        <div>运动使我健康</div>
    </div>
    <script>
        var lis = document.querySelectorAll('li')
        var divs = document.querySelectorAll('.box>div')
        function changeTab(index) {
            for (var i = 0; i < lis.length; i++) {
                lis[i].className = ''
                divs[i].className = ''
            }
            lis[index].className = 'active'
            divs[index].className = 'active'

        }

        //自定义属性方式
        for (var i = 0; i < lis.length; i++) {
            lis[i].myindex = i
            lis[i].onclick = function () {
                changeTab(this.myindex)
            }
        }
        //let方式
        for (let i = 0; i < lis.length; i++) {
            lis[i].onclick = function () {
                changeTab(i)
            }
        }
        //闭包方式
        for (var i = 0; i < lis.length; i++) {

            (function (n) {
                lis[i].onclick = function () {
                    changeTab(n)
                }
            })(i)
        }



    </script>
</body>

</html>